#include "hc05_uart.h"
#include "delay.h"

UART_IO_RECV uartio_buf;

void hc05_uart_init(void)
{
	GPIO_InitTypeDef GPIO_Inittypedef;
	EXTI_InitTypeDef EXTI_InitStructure;		//EXTIx中断线配置
	NVIC_InitTypeDef NVIC_InitStructure;		//EXTIx中断向量配置
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//开启时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);
	
	
	GPIO_Inittypedef.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Inittypedef.GPIO_Pin = GPIO_Pin_12;
	GPIO_Inittypedef.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_Inittypedef);
	GPIO_Inittypedef.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_Inittypedef.GPIO_Pin = GPIO_Pin_13;
	GPIO_Init(GPIOB,&GPIO_Inittypedef);
	GPIO_SetBits(GPIOB,GPIO_Pin_12|GPIO_Pin_13);
	
	//绑定中断线
  GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource13);
	
	EXTI_InitStructure.EXTI_Line=EXTI_Line13;
  EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
  EXTI_InitStructure.EXTI_LineCmd=ENABLE;
  EXTI_Init(&EXTI_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_Init(&NVIC_InitStructure);
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;//结构体定义
	TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;//设置时钟分割,这里是不分割
	TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //TIM向上计数模式
	TIM_TimeBaseInitStructure.TIM_Period= 108;//设置在下一个更新事件装入活动的自动重装载寄存器周期的值
	TIM_TimeBaseInitStructure.TIM_Prescaler=71;//预分频值
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0;//定时8.5US秒，进入一次中断
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);//初始化TIM2
	
	TIM_ClearFlag(TIM3,TIM_FLAG_Update);//中断清除，防止一开始就进入一次定时器
	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//开启定时器中断
	
	NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;//设置中断源
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//中断源使能
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//抢占优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;//响应优先级
	NVIC_Init(&NVIC_InitStructure); //调用NVIC初始化函数
	
	TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
	TIM_Cmd(TIM3,ENABLE);//定时器2使能
	TIM_Cmd(TIM3,DISABLE);//定时器2失能
	uartio_buf.head = 0;
	uartio_buf.tail = 0;
}

//检测起始位
void EXTI15_10_IRQHandler(void)
{
	
	if (RESET != EXTI_GetITStatus(EXTI_Line13))
	{
		EXTI->IMR &= ~(1<<13);//关闭中段
		if(RX_BIT == 0)
		{
			TIM_SetCounter(TIM3,0);
			TIM_Cmd(TIM3,ENABLE);//定时器2使能
		}
		EXTI_ClearITPendingBit(EXTI_Line13);
	}
}


//获取数据
void TIM3_IRQHandler(void)
{
	static uint8_t bitcnt = 0;
	static uint8_t value = 0;
	if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET)//检查指定的TIM中断发生与否:TIM 中断源
	{
		if(RX_BIT)value |= (0x01<<bitcnt);
		else value &= ~(0x01<<bitcnt);
		bitcnt++;
		if(bitcnt == 8)
		{
			uartio_buf.uart_io_buf[uartio_buf.head++] = value;
			uartio_buf.head %= 128;
			bitcnt = 0;
			TIM_Cmd(TIM3,DISABLE);//定时器2失能
			EXTI->IMR |=1<<13;
		}
		TIM_ClearITPendingBit(TIM3,TIM_IT_Update);//清除TIMx的中断待处理位:TIM 中断源 
	}
}

//发送字符串
void hc05_send_str(char *str)
{
  EXTI->IMR &= ~(1<<13);//关闭中段
	while(*str)
	{
		hc05_send_byte(*str);
		str++;
	}
  EXTI->IMR |=1<<13;
}

void hc05_send_byte(uint8_t data)
{
	uint8_t i;
	TX_RESET//拉低TX 起始位
	delay_us(BAUD_115200);
	
	for(i=0;i<8;i++)
	{
		if(data&0x01)TX_SET//先发LSB
		else TX_RESET
		data>>=1;
		delay_us(BAUD_115200);
	}
	
	TX_SET//停止位
	delay_us(BAUD_115200);
}

